import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:quiver/strings.dart';
import 'package:shared_preferences/shared_preferences.dart';

/*
 * @author jingsong.chen, QQ:77132995, email:kazeik@163.com
 * 类说明:
 */
class Utils {
  static int limit = 10;
  static Color nomalColor = Color(0xff241d85);
  static Color bgColor = Color(0xfff5f5f5);
  static Color hintColor = Color(0xffBFC2CC);
  static const String receiptPrintMsg = "com.cs.jingsong/eatbusiness.receipt";

  static const String wxAppId = "wx435e837f1b0a5ae7";
  static const String wxUniversalLink = "https://www.e-at.com/cloud-business/";

  static List<String> imgMenus = ["拍照", "相册"];

  static String getImgPath(String name, {String format: 'png'}) {
    return 'assets/images/$name.$format';
  }

  static String setUrlImg(String url) {
    return '${url}h_180,w_180';
  }

  static Future<void> pop() async {
    await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  }

  static logs(String msg) {
    // if (Config.env == Env.DEV) {
    debugPrint(msg);
    // }
  }

  static String checkValue(String value) {
    return value == null || isEmpty(value) || value == "null" ? "" : value;
  }


  static bool isChinaPhoneLegal(String str) {
    return new RegExp(
            '^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[8-9])|(147,145))\\d{8}\$')
        .hasMatch(str);
  }

  static showToast(String msg) {
    Fluttertoast.showToast(
        msg: msg,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        backgroundColor: Colors.grey.shade300,
        textColor: Colors.black,
        fontSize: 16.0);
  }

  /*
   * bytedata转String
   */
  static String getStringFromBytes(ByteData data) {
    final buffer = data.buffer;
    var list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    return utf8.decode(list);
  }

  static String checkValueToDouble(dynamic value, {int? length}) {
    int numlength = length == null ? 2 : length;
    String tempPrice = numlength == 4 ? ".0000" : ".00";
    if (value == null) return "0$tempPrice";
    String oldValue = value.toString();
    if (num.parse(oldValue) == null) return "0$tempPrice";
    int point = oldValue.indexOf(".");
    if (point == -1) {
      return "0$tempPrice";
    } else {
      String beforPoint = oldValue.substring(point + 1, oldValue.length);
      if (beforPoint.length == numlength) {
        return oldValue;
      } else {
        String zero = "";
        for (int i = numlength - beforPoint.length; i > 0; i--) {
          zero = zero + "0";
        }
        return "$oldValue$zero";
      }
    }
  }
}
